package com.resou.reader.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.resou.reader.R;

/**
 * Created by ${Garrett} on 2018/9/13.
 * Contact me krouky@outlook.com
 */
public class ChoiceView extends View {

    private Paint mPaint;
    private Bitmap interiorBp;
    private int bgColor;

    public ChoiceView(Context context) {
        this(context, null);
    }

    public ChoiceView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
        interiorBp = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_choice);
    }

    public ChoiceView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(context.getResources().getDimension(R.dimen.qb_px_2));

        Drawable drawable = getBackground();
        ColorDrawable colorDrawable = (ColorDrawable) drawable;
        bgColor = colorDrawable.getColor();
        setBackground(new ColorDrawable(Color.TRANSPARENT));
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.DST_OUT);
        mPaint.setColor(bgColor);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, Math.min(getWidth(), getHeight()) / 2, mPaint);
        if (isChoice) {
            mPaint.setShader(null);
            mPaint.setColor(Color.RED);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeWidth(getResources().getDimension(R.dimen.qb_px_2));
            canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint);
            canvas.drawBitmap(interiorBp, getWidth() / 2 - interiorBp.getWidth() / 2, getHeight() / 2 - interiorBp.getHeight() / 2, null);
        }
    }

    private boolean isChoice;

    public void setChoice(boolean isChoice) {
        this.isChoice = isChoice;
        invalidate();
    }
}
